package com.lagou.server;

import com.lagou.model.Student;
import com.lagou.model.StudentMessage;
import com.lagou.model.userMessage;

import java.io.IOException;
import java.util.List;

/**
 * 编程实现服务器的主功能
 */
public class ServerView {

    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;

    /**
     * 通过构造方法实现成员变量的初始化
     * @param sic
     */
    public ServerView(ServerInitClose sic, ServerDao sd) {
        this.sic = sic;
        this.sd = sd;
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        userMessage tum = (userMessage) sic.getOis().readObject();
        System.out.println("接收到的消息是：" + tum);
        // 调用方法实现管理员账号和密码信息的校验
        if (sd.serverManagerCheck(tum.getUser())) {
            tum.setType("success");
        } else {
            tum.setType("fail");
        }
        // 将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送校验结果成功！");
        if (sd.serverManagerCheck(tum.getUser())) {
            while (true) {
                tum = (userMessage) sic.getOis().readObject();
                System.out.println("接收到的消息是：" + tum);
                if (tum.getType().equals("studnetManager")){
                    while (true){
                        StudentMessage stum = (StudentMessage) sic.getOis().readObject();
                        System.out.println("接收到的消息是：" + stum);
                        if (stum.getType().equals("addStudent")) {
                            Student student = stum.getStudent();
                            List<Student> stuList = sic.getStuList();
                            //判断学生是否存在
                            for (int i = 0; i < stuList.size() ; i++) {
                                if (stuList.get(i).getName().equals(student.getName())) {
                                    //添加失败，并发给客户端
                                    stum = new StudentMessage("fail",null);
                                    sic.getOos().writeObject(stum);
                                    //服务器继续处于学生管理处理中
                                    continue;
                                }
                            }
                            //通过了检验，进行添加
                            stuList.add(student);
                            //告诉客户端添加成功
                            stum = new StudentMessage("success",null);
                            sic.getOos().writeObject(stum);
                        }else if(stum.getType().equals("getAllStudent")){
                            //发给客户端所有学员信息
                            sic.getOos().reset();
                            sic.getOos().writeObject(sic.getStuList());
                        }else if(stum.getType().equals("stuexit")){
                            //保存文件的操作
                            sic.saveStuList();
                            break;
                        }
                    }
                }else if (tum.getType().equals("testPaperManager")){

                }else if (tum.getType().equals("exit")){

                    break;
                }
            }
        }


    }
}